package database.local;

import android.os.Parcel;
import android.os.Parcelable;
import server.SaveZoneHttpConnection;
import server.ServerObject;

public class Comment extends ServerObject implements Comparable<Comment>, Parcelable {
	private String date_time;
	private String comment_str;
	private String deal_item;
	private int rate;
	private int cost_level;
	
	public Comment() {
		super();
		setSource_type("user");
		setSource("");
		setDate_time(null);
		setComment_str(null);
		setDeal_item(null);
		setRate(0);
		setCost_level(0);
	}

	/**
	 * @return the date_time
	 */
	public String getDate_time() {
		int round_index = date_time.lastIndexOf(':');
		if (round_index == -1) return "";
		return date_time.substring(0, round_index);
	}

	/**
	 * @param date_time the date_time to set
	 */
	public void setDate_time(String date_time) {
		this.date_time = date_time;
	}

	/**
	 * @return the comment_str
	 */
	public String getComment_str() {
		return comment_str;
	}

	/**
	 * @param comment_str the comment_str to set
	 */
	public void setComment_str(String comment_str) {
		this.comment_str = comment_str;
	}

	/**
	 * @return the deal_item
	 */
	public String getDeal_item() {
		return deal_item;
	}

	/**
	 * @param deal_item the deal_item to set
	 */
	public void setDeal_item(String deal_item) {
		this.deal_item = deal_item;
	}

	/**
	 * 
	 * @param rate - int between 1 to 5 
	 */
	public void rateMe(int rate) {
		if (rate < 1 || rate > 5) return;
		SaveZoneHttpConnection.rateComment(this, rate - 1);
	}
	
	public int getRate() {
		return this.rate + 1;
	}
	public void setRate(int rate) {
		this.rate = rate;
	}
	
	public int getCost_level() {
		return this.cost_level + 1;
	}
	public void setCost_level(int cost_level) {
		this.cost_level = cost_level;
	}
	
	
	/*compared by rating*/
	public int compareTo(Comment other) {
		if (other == null) return -1;
		if (this.getRate() > other.getRate()) return -1;
		else return 1;
	}

	/* Parcelable implements methods */
	
	public Comment (Parcel in){
		readFromParcel(in);
	}
	
	public void readFromParcel(Parcel in) {
		super.readFromParcel(in);
		setSource(in.readString());
		setSource_type(in.readString());
		date_time = in.readString();
		comment_str = in.readString();
		deal_item = in.readString();
		rate = in.readInt();
		cost_level = in.readInt();
	}

	public int describeContents() {
		return 0;
	}

	public void writeToParcel(Parcel dest, int flags) {
		super.writeToParcel(dest, flags);
		dest.writeString(getSource());
		dest.writeString(getSource_type());
		dest.writeString(date_time);
		dest.writeString(comment_str);
		dest.writeString(deal_item);
		dest.writeInt(rate);
		dest.writeInt(cost_level);
	}
	
	public static final Parcelable.Creator CREATOR =
	    	new Parcelable.Creator() {
	            public Comment createFromParcel(Parcel in) {
	                return new Comment(in);
	            }
	 
	            public Comment[] newArray(int size) {
	                return new Comment[size];
	            }
	        };

}
